


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Vendor_Distribution_Meters') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Vendor_Distribution_Meters
GO

create view v_tb_Vendor_Distribution_Meters as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Vendor_Distribution_Meters_MeterId] as [Vendor_Distribution_Meters_MeterId]
	,[Vendor_Distribution_PoleFk] as [Vendor_Distribution_PoleFk]
	,[Latitude] as [Latitude]
	,[Longitude] as [Longitude]
	,[Number] as [Number]
	,[ImageFileName] as [ImageFileName]
	,[Comments] as [Comments]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [ImageFileName]), 255) as [FriendlyName]
from
	tb_Vendor_Distribution_Meters
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Test
GO


create proc up_VendorDistributionMeter_Test
	@Vendor_Distribution_Meters_MeterId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Vendor_Distribution_Meters_MeterId] 
	from [tb_Vendor_Distribution_Meters] source
	where [source].[Vendor_Distribution_Meters_MeterId] = @Vendor_Distribution_Meters_MeterId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Select
GO

create proc up_VendorDistributionMeter_Select
	 @Vendor_Distribution_Meters_MeterId int = null 
	,@Vendor_Distribution_PoleFk int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Number nvarchar(25) = null 
	,@ImageFileName nvarchar(100) = null 
	,@Comments ntext = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select * from v_tb_Vendor_Distribution_Meters
where
		IsNull([v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_Meters_MeterId], -1) = Coalesce(@Vendor_Distribution_Meters_MeterId, [v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_Meters_MeterId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_PoleFk], -1) = Coalesce(@Vendor_Distribution_PoleFk, [v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_PoleFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Vendor_Distribution_Meters].[Latitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Vendor_Distribution_Meters].[Longitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Number], '') = Coalesce(@Number, [v_tb_Vendor_Distribution_Meters].[Number], '')
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[ImageFileName], '') = Coalesce(@ImageFileName, [v_tb_Vendor_Distribution_Meters].[ImageFileName], '')
	AND	IsNull(Convert(varchar(2000), [v_tb_Vendor_Distribution_Meters].[Comments]), '') = Coalesce(Convert(varchar(2000), @Comments), Convert(varchar(2000), [v_tb_Vendor_Distribution_Meters].[Comments]), '')
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Select_Top1
GO

create proc up_VendorDistributionMeter_Select_Top1
	 @Vendor_Distribution_Meters_MeterId int = null 
	,@Vendor_Distribution_PoleFk int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Number nvarchar(25) = null 
	,@ImageFileName nvarchar(100) = null 
	,@Comments ntext = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected top 1 record based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select TOP 1 * from v_tb_Vendor_Distribution_Meters
where
		IsNull([v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_Meters_MeterId], -1) = Coalesce(@Vendor_Distribution_Meters_MeterId, [v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_Meters_MeterId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_PoleFk], -1) = Coalesce(@Vendor_Distribution_PoleFk, [v_tb_Vendor_Distribution_Meters].[Vendor_Distribution_PoleFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Vendor_Distribution_Meters].[Latitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Vendor_Distribution_Meters].[Longitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[Number], '') = Coalesce(@Number, [v_tb_Vendor_Distribution_Meters].[Number], '')
	AND	IsNull([v_tb_Vendor_Distribution_Meters].[ImageFileName], '') = Coalesce(@ImageFileName, [v_tb_Vendor_Distribution_Meters].[ImageFileName], '')
	AND	IsNull(Convert(varchar(2000), [v_tb_Vendor_Distribution_Meters].[Comments]), '') = Coalesce(Convert(varchar(2000), @Comments), Convert(varchar(2000), [v_tb_Vendor_Distribution_Meters].[Comments]), '')
GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Insert
GO

create proc up_VendorDistributionMeter_Insert
	 @Vendor_Distribution_Meters_MeterId int =null OUTPUT  
	,@Vendor_Distribution_PoleFk int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Number nvarchar(25) = null  
	,@ImageFileName nvarchar(100) = null  
	,@Comments ntext = null  
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Vendor_Distribution_Meters 
	(
	
	 [Vendor_Distribution_PoleFk] 
	,[Latitude] 
	,[Longitude] 
	,[Number] 
	,[ImageFileName] 
	,[Comments] 
	)
	values
	(
	
	 @Vendor_Distribution_PoleFk 
	,@Latitude 
	,@Longitude 
	,@Number 
	,@ImageFileName 
	,@Comments 
	)
set @Vendor_Distribution_Meters_MeterId = scope_identity()
select * from v_tb_Vendor_Distribution_Meters where Vendor_Distribution_Meters_MeterId = @Vendor_Distribution_Meters_MeterId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Update
GO


create proc up_VendorDistributionMeter_Update
	 @Vendor_Distribution_Meters_MeterId int  
	,@Vendor_Distribution_PoleFk int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Number nvarchar(25) = null  
	,@ImageFileName nvarchar(100) = null  
	,@Comments ntext = null  
	,@OriginalVendor_Distribution_Meters_MeterId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalVendor_Distribution_Meters_MeterId is null
	set @OriginalVendor_Distribution_Meters_MeterId = @Vendor_Distribution_Meters_MeterId

-- test
declare @Result bit
execute @Result = up_VendorDistributionMeter_Test @OriginalVendor_Distribution_Meters_MeterId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_Distribution_Meters_MeterId)
	raiserror(505060, 16, 1, 'VendorDistributionMeter', @x)
	return
end

-- proceed
update tb_Vendor_Distribution_Meters 
set

	 [Vendor_Distribution_PoleFk] =  @Vendor_Distribution_PoleFk
	,[Latitude] =  @Latitude
	,[Longitude] =  @Longitude
	,[Number] =  @Number
	,[ImageFileName] =  @ImageFileName
	,[Comments] =  @Comments
	
from tb_Vendor_Distribution_Meters source
where
	[source].[Vendor_Distribution_Meters_MeterId] = @OriginalVendor_Distribution_Meters_MeterId


select * from v_tb_Vendor_Distribution_Meters where Vendor_Distribution_Meters_MeterId = @Vendor_Distribution_Meters_MeterId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionMeter_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionMeter_Delete
GO


create proc up_VendorDistributionMeter_Delete
	@Vendor_Distribution_Meters_MeterId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_VendorDistributionMeter_Test @Vendor_Distribution_Meters_MeterId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_Distribution_Meters_MeterId)
	raiserror(505060, 16, 1, 'VendorDistributionMeter', @x)
	return
end

-- proceed
delete from tb_Vendor_Distribution_Meters 
where [Vendor_Distribution_Meters_MeterId] = @Vendor_Distribution_Meters_MeterId

GO


